# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import MySQLdb

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from detailModelTest import InterestZhihuUser, Relationship, Base


class ZhihuSpiderPipeline(object):
    def process_item(self, item, spider):
        return item


# 字段存储到Mysql
class MySQLStorePipeline(object):
    '''
    fix scrapy in linux
    error: command 'gcc' failed with exit status 1 的解决办法
    用pip安装软件时提示：error: command 'gcc' failed with exit status 1
    easy_install也提示一样的错误：error: Setup script exited with error: command 'gcc' failed with exit status 1
    一般是缺少python-dev包
    apt-get install python-dev或yum install python-devel这样就行了
    fix charset in simple chinese
    ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    '''

    def process_item(self):
        print "start insert data"

        engine = create_engine('mysql+mysqldb://root:toor123@115.29.148.86:3306/zhihu?charset=utf8', echo=True)
        Base.metadata.create_all(engine)  # 创建数据库表      这部分可以提取出去
        dbsession = sessionmaker(bind=engine)
        session = dbsession()

        interest = InterestZhihuUser("xxxxx", 'xxxx000')

        try:
            session.add(interest)
            session.commit()
        except Exception:
            pass


if __name__ == "__main__":
    m = MySQLStorePipeline()
    m.process_item()
